package com.yw.thesimpllestplayer.ui;

import android.media.MediaFormat;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.yw.thesimpllestplayer.BaseActivity;
import com.yw.thesimpllestplayer.R;
import com.yw.thesimpllestplayer.extractor.AudioExtractor;
import com.yw.thesimpllestplayer.extractor.IExtractor;
import com.yw.thesimpllestplayer.extractor.VideoExtractor;

/**
 * @ProjectName: TheSimpllestplayer
 * @Package: com.yw.thesimpllestplayer.ui
 * @ClassName: MediaExtractorActivity
 * @Description: 媒体提取器，相关注释：https://blog.csdn.net/xgxmwang/article/details/80882016
 * @Author: wei.yang
 * @CreateDate: 2021/11/3 10:15
 * @UpdateUser: 更新者：wei.yang
 * @UpdateDate: 2021/11/3 10:15
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MediaExtractorActivity extends BaseActivity {
    private static final String TAG = "MediaExtractorActivity";
    private TextView tvVideoInfo;
    private TextView tvAudioInfo;
    private IExtractor mVideoExtractor, mAudioExtractor;
    private Button btnChoiceVideo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_extractor);
        initViews();
    }

    private void initViews() {
        tvVideoInfo = findViewById(R.id.tvVideoInfo);
        tvAudioInfo = findViewById(R.id.tvAudioInfo);
        btnChoiceVideo = findViewById(R.id.btnChoiceVideo);
        btnChoiceVideo.setOnClickListener(v -> {
            choiceVideo();
        });

    }

    private void initDatas(String filePath) {
        mVideoExtractor = new VideoExtractor(filePath);
        mAudioExtractor = new AudioExtractor(filePath);
        //开始展示相关数据
        StringBuilder sbVideo = new StringBuilder();
        MediaFormat formatVideo = mVideoExtractor.getFormat();
        if (formatVideo == null) {
            Log.e(TAG, "媒体格式为空");
        }
        String mimeType = null;
        try {
            mimeType = formatVideo.getString(MediaFormat.KEY_MIME);//媒体格式
        } catch (Exception e) {
            e.printStackTrace();
        }
        int maxBufferSize = 0;
        try {
            maxBufferSize = formatVideo.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);//缓冲区最大大小
        } catch (Exception e) {
            e.printStackTrace();
        }
        int bitRate = 0;
        try {
            bitRate = formatVideo.getInteger(MediaFormat.KEY_BIT_RATE);//码率
        } catch (Exception e) {
            e.printStackTrace();
        }
        int width = 0;
        try {
            width = formatVideo.getInteger(MediaFormat.KEY_WIDTH);//视频宽度
        } catch (Exception e) {
            e.printStackTrace();
        }
        int height = 0;
        try {
            height = formatVideo.getInteger(MediaFormat.KEY_HEIGHT);//视频高度
        } catch (Exception e) {
            e.printStackTrace();
        }
        int fps = 0;
        try {
            fps = formatVideo.getInteger(MediaFormat.KEY_FRAME_RATE);//fps
        } catch (Exception e) {
            e.printStackTrace();
        }

        sbVideo
                .append("视频参数：").append("\n")
                .append("mimeType:").append(mimeType).append("\n")
                .append("bitRate:").append(bitRate).append("\n")
                .append("maxBufferSize:").append(maxBufferSize).append("\n")
                .append("width:").append(width).append("\n")
                .append("height:").append(height).append("\n")
                .append("fps:").append(fps).append("\n");
        Log.e(TAG, sbVideo.toString());
        tvVideoInfo.setText(sbVideo.toString());


        StringBuilder sbAudio = new StringBuilder();
        MediaFormat audioFormat = mAudioExtractor.getFormat();
        int channelCount = 0;
        try {
            channelCount = audioFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int audioBitrate = 0;
        try {
            audioBitrate = audioFormat.getInteger(MediaFormat.KEY_BIT_RATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int sampleRate = 0;
        try {
            sampleRate = audioFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
        } catch (Exception e) {
            e.printStackTrace();
        }


        sbAudio
                .append("音频参数：").append("\n")
                .append("channelCount:").append(channelCount).append("\n")
                .append("audioBitrate:").append(audioBitrate).append("\n")
                .append("sampleRate:").append(sampleRate).append("\n");
        Log.e(TAG, sbAudio.toString());
        tvAudioInfo.setText(sbAudio.toString());
    }

    @Override
    public void videoPathCallback(String videoPath) {
        Log.e(TAG, videoPath);
        initDatas(videoPath);
    }
}
